#import "UIColor+Extension.h"

@implementation UIColor (Extension)

+ (UIColor *)colorWithHexString:(NSString *)hexString {
    return [self colorWithHexString:hexString alpha:1.0f];
}

+ (UIColor *)colorWithHexString:(NSString *)hexString alpha:(CGFloat)alpha {
    NSString *colorString ;
    
    if ([hexString containsString:@"#"]) {
        colorString =  [[hexString stringByReplacingOccurrencesOfString:@"#" withString:@""] uppercaseString];
    } else {
        colorString = hexString;
    }
   
    CGFloat red, blue, green;
    
    if ([colorString length] == 6) {
        red   = [self colorComponentFrom:colorString start:0 length:2];
        green = [self colorComponentFrom:colorString start:2 length:2];
        blue  = [self colorComponentFrom:colorString start:4 length:2];
    } else {
        return nil;
    }
    
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

+ (CGFloat)colorComponentFrom:(NSString *)string start:(NSUInteger)start length:(NSUInteger)length {
    NSString *substring = [string substringWithRange:NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat:@"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString:fullHex] scanHexInt:&hexComponent];
    return hexComponent / 255.0;
}

// 常用背景灰色 #F4F4F4
+ (UIColor *)backgroundGrayColor {
    return [self colorWithHexString:@"#F4F4F4"];
}

// 主题色，可以根据需求修改
+ (UIColor *)mainColor {
    return [self colorWithHexString:@"#3271ED"];
}

// 文字灰色
+ (UIColor *)textGrayColor {
    return [self colorWithHexString:@"#999999"];
}

@end 
